package com.example.lyl.yuejian.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.NotifyEventBean;
import com.example.lyl.yuejian.bean.SelectUserInfoBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.util.SharePreferenceUtils;
import com.example.lyl.yuejian.view.LoginActivity;
import com.example.lyl.yuejian.view.ModifyUserInfoActivity;
import com.example.lyl.yuejian.view.home.MatchOrderActivity;
import com.example.lyl.yuejian.view.my.MyMatchOrderListActivity;
import com.example.lyl.yuejian.view.my.MyOrderListActivity;
import com.example.lyl.yuejian.view.my.MyPhotoActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.telephone)
    TextView telephone;
    @BindView(R.id.adress)
    TextView adress;
    @BindView(R.id.wddd_btn)
    Button wdddBtn;
    @BindView(R.id.wdgp_btn)
    Button wdgpBtn;
    @BindView(R.id.wdst_btn)
    Button wdstBtn;
    @BindView(R.id.xgxx_btn)
    Button xgxxBtn;
    @BindView(R.id.tcdl_btn)
    Button tcdlBtn;
    @BindView(R.id.tx_iv)
    CircleImageView tx_iv;
    Unbinder unbinder;

    public MyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        toolbar.setTitle("我的");

        name.setText(SharePreferenceUtils.getString(getContext(), "username", ""));
        sex.setText(SharePreferenceUtils.getString(getContext(),"sex",""));
        adress.setText(SharePreferenceUtils.getString(getContext(),"adress",""));
        telephone.setText(SharePreferenceUtils.getString(getContext(),"tel",""));
        if (!SharePreferenceUtils.getString(getContext(),"imgPath","").equals("")){
            Glide.with(getContext()).load(SharePreferenceUtils.getString(getContext(),"imgPath","")).into(tx_iv);
        }else {
            getUserInfo();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.wddd_btn, R.id.wdgp_btn, R.id.wdst_btn, R.id.xgxx_btn, R.id.tcdl_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wddd_btn:
                startActivity(new Intent(getContext(), MyOrderListActivity.class));
                break;
            case R.id.wdgp_btn:
                startActivity(new Intent(getContext(), MyMatchOrderListActivity.class));
                break;
            case R.id.wdst_btn:
                startActivity(new Intent(getContext(), MyPhotoActivity.class));
                break;
            case R.id.xgxx_btn:
                startActivity(new Intent(getActivity(), ModifyUserInfoActivity.class));
                break;
            case R.id.tcdl_btn:
                SharePreferenceUtils.deleShare(getContext(),"username");
                SharePreferenceUtils.deleShare(getContext(),"sex");
                SharePreferenceUtils.deleShare(getContext(),"adress");
                SharePreferenceUtils.deleShare(getContext(),"tel");
                SharePreferenceUtils.deleShare(getContext(),"imgPath");
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
        }
    }

    @Subscribe
    public void onEventMainThread(NotifyEventBean event) {
        if (event.getMsg().equals("刷新用户信息")){
            getUserInfo();
        }
    }

    private void getUserInfo() {
        NetTool.getInstance().getApi()
                .selectUserInfo(SharePreferenceUtils.getInt(getContext(),"uid",0))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<SelectUserInfoBean>() {
                    @Override
                    public void accept(SelectUserInfoBean selectUserInfoBean) throws Exception {
                        sex.setText(selectUserInfoBean.getData().getUsex());
                        telephone.setText(selectUserInfoBean.getData().getUtel());
                        adress.setText(selectUserInfoBean.getData().getUadress());
                        Glide.with(getContext()).load("http://" + selectUserInfoBean.getData().getUpic()).placeholder(R.drawable.peopleicon).into(tx_iv);
                        SharePreferenceUtils.putString(getContext(),"sex",sex.getText().toString());
                        SharePreferenceUtils.putString(getContext(),"tel",telephone.getText().toString());
                        SharePreferenceUtils.putString(getContext(),"adress",adress.getText().toString());
                        SharePreferenceUtils.putString(getContext(),"imgPath","http://" + selectUserInfoBean.getData().getUpic());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }
}
